import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const state = {
  userInfo:null,
  tempAddress:null,
  location:{}
}

const getters = {
  userInfo:state=>state.userInfo,
  tempAddress:state=>state.tempAddress,
  location:state=>state.location,
  commonAddress:state=>state.userInfo.common_address
}

const mutations = {
  setUserInfo(state,userInfo){
    state.userInfo=userInfo?userInfo:null
  },
  setTempAddress(state,tempAddress){
    state.tempAddress=tempAddress;
  },
  setLocation(state,location){
    state.location=location;
  },
  setCommonAddress(state,newAddress){
    state.userInfo.common_address.push(newAddress);
  }
}

const actions = {
  setUserInfo({commit},userInfo){
    commit('setUserInfo',userInfo)
  },
  setTempAddress({commit},tempAddress){
    commit('setTempAddress',tempAddress);
  },
  setLocation({commit},location){
    commit("setLocation",location)
  },
  setCommonAddress({commit},newAddress){
    commit('setCommonAddress',newAddress)
  }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
